/***
This do-file creates a binscatter of changes in low-wage employment vs workplace
rent at the ZIP-level.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Changes in low-income employment vs workplace rent.yaml"
cap erase "${root}/results/paper numbers/`category'/Change in Low-Income Employment vs Workplace Rent - ZIP.yaml"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

project, uses("${root}/data/web/data/Earnin - ZCTA - 2020.csv")
import delimited "${root}/data/web/data/Earnin - ZCTA - 2020.csv", clear

* Merge ZIP-level covariates
gen zcta5a = zcta
gen zcta5 = zcta
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
merge m:1 zcta using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", keep(1 3) nogen
rename (pop_2014_2018_est med_2br_2014_2018_est medhhinc_2014_2018_est)(pop_2018 median_2br_rent_2018 med_inc_2018)

project, uses("${root}/data/derived/LODES/ZCTA mean work rent low_pay_jobs.dta")
merge 1:1 zcta using "${root}/data/derived/LODES/ZCTA mean work rent low_pay_jobs.dta", keep(1 3) nogen keepusing(med_2br_rent_work_zcta low_pay_jobs)

* Merge counties
preserve
	project, uses("${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt")
	import delimited "${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt", encoding(ISO-8859-2) clear
	sort zcta5 poppt
	bysort zcta5 (poppt): keep if _n == _N // only keeps the county where most of the zcta lives
	gen county_fips = 1000*state + county
	keep zcta5 county_fips
	tempfile zcta_county_CW
	save `zcta_county_CW'
restore

merge 1:1 zcta5 using `zcta_county_CW', keep(1 3) nogen

* Get income quantiles
bys zcta5 : gen first = _n == 1
xtile q_inc = med_inc_2018 [w = pop_2018] if first == 1, n(4)
bys zcta5 (q_inc): replace q_inc = q_inc[1]
assert !mi(q_inc) if !mi(med_inc_2018)

gen norm_emp_100 = -100 + emp_incq1_apr2020 * 100

* Create fig
local x_title "Average Two Bedroom Monthly Rent in 2014-2018 in Workplace ZIP ($)"

reg norm_emp_100 med_2br_rent_work_zcta ///
 [w = low_pay_jobs], vce(cluster county_fips)

local beta = _b[med_2br_rent_work_zcta] * 1000
local display_beta: display %4.2f `beta'
local std_err = _se[med_2br_rent_work_zcta] * 1000
local display_std_err: display %4.2f `std_err'

local text "text(-30 580 "Slope = `display_beta'%/$1000 (s.e. = `display_std_err')", place(ne) just(left) size(medlarge) color(gs8))"

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

binscatter norm_emp_100 med_2br_rent_work_zcta ///
[w = low_pay_jobs] ///
	, reportreg ///
	xtitle("`x_title'") ///
	xscale(range("580 2030")) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800" 2000 "$2,000") ///
	ytitle("Change in Employment (%)" "from January to April 2020") ///
	ylabel(-15 "-15%" -20 "-20%" -25 "-25%" -30 "-30%", nogrid) ///
	`text' ///
	${title_`version'}
oi_graph_export "${root}/results/Employment/Change in Low-Income Employment vs. Workplace Rent", type(${fig_type})

* Black and white version for QJE
binscatter norm_emp_100 med_2br_rent_work_zcta ///
[w = low_pay_jobs] ///
	, reportreg mcolor(gs0) lcolor(gs8) ///
	xtitle("`x_title'") ///
	xscale(range("580 2030")) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800" 2000 "$2,000") ///
	ytitle("Change in Employment (%)" "from January to April 2020") ///
	ylabel(-15 "-15%" -20 "-20%" -25 "-25%" -30 "-30%", nogrid) ///
	`text' ///
	${title_`version'}

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_9a.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_9a.svg")

yamlout using "${root}/results/paper numbers/`category'/Changes in low-income employment vs workplace rent.yaml" , ///
	key("emp_wprent_slope") ///
	comment("Slope (%/$1000)") ///
	value(`display_beta') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Changes in low-income employment vs workplace rent.yaml", ///
	key("emp_wprent_se") ///
	comment("SE") ///
	value(`display_std_err') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Changes in low-income employment vs workplace rent.yaml")
